package com.vancouver.knows.ui.activity.login;

import com.pangu.service.ActionException;
import com.pangu.task.IResultReceiver;
import com.pangu.task.mark.ATaskMark;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.ui.common.MyAutoCompleteTextView;
import com.vancouver.knows.util.InputMethodManger;
import com.vancouver.knows.util.PhoConstants;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.task.mark.LoginTaskMark;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements IResultReceiver{

	private Context mContext;
	@Bind(R.id.cancel)
	TextView mCancel;
	@Bind(R.id.register)
	TextView mRegister;
	@Bind(R.id.area_code)
	TextView mAreaCode;
	@Bind(R.id.area)
	TextView mArea;
	@Bind(R.id.btn_login)
	Button mLoginBtn;
	@Bind(R.id.phone_or_email)
	MyAutoCompleteTextView mPhoneOrEmailInput;
	@Bind(R.id.password)
	MyAutoCompleteTextView mPasswordInput;
	@Bind(R.id.forget_password)
	TextView mForgetPsd;
	private InputMethodManger inputMethodManger;
	private LoadingDialog loadingDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		mContext = LoginActivity.this;
		setViews();
		setListeners();
	}

	private void setViews() {
		inputMethodManger = new InputMethodManger();
		registerChooseCountryBroastcast();
		loadingDialog = new LoadingDialog(mContext);
		//mPhoneOrEmailInput.setText("demo@vancoolver.ca");
		//mPasswordInput.setText("demo");
		//mPhoneOrEmailInput.setText("7788855995");
		////mPasswordInput.setText("1234");
		
	}
	
	private void setListeners(){
		mCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, PhoneSecurityCodeActivity.class);
				intent.putExtra("COMMON_TIPS", mContext.getString(R.string.phone_register).toString());
				mContext.startActivity(intent);
			}
		});
		
		mArea.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ChooseCountryActivity.class);
				intent.putExtra("COMMON_TIPS", mContext.getString(R.string.choose_country).toString());
				intent.putExtra("CURRENT_OPEN_ACTIVITY", PhoConstants.CURRENT_OPEN_FROM_LOGIN);
				mContext.startActivity(intent);
			}
		});
		
		mLoginBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mPhoneOrEmailInput.getText().toString().trim().length() == 0 ){
					Toast.makeText(mContext, mContext.getText(R.string.login_Tips), Toast.LENGTH_SHORT).show();
					return;
				}
				if (mPhoneOrEmailInput.getText().toString().trim().length() > 0 
						&& mPasswordInput.getText().toString().trim().length() == 0){
					Toast.makeText(mContext, mContext.getText(R.string.login_input_psd_tips), Toast.LENGTH_SHORT).show();
					return;
				}
				BusinessModule bModule = imContext.getBusinessModule();
				LoginTaskMark taskMark = bModule.getTaskMarkPool().getLoginTaskMark();
				if (mPhoneOrEmailInput.getText().toString().trim().contains("@")
						&& mPhoneOrEmailInput.getText().toString().trim().contains(".")){
					bModule.getServiceWrapper().login(LoginActivity.this, taskMark
							, mPhoneOrEmailInput.getText().toString().trim(),"", mPasswordInput.getText().toString().trim(), null);
				}else{
					String code = getAreaCode();
					bModule.getServiceWrapper().login(LoginActivity.this, taskMark
							, "",code + mPhoneOrEmailInput.getText().toString().trim(), mPasswordInput.getText().toString().trim(), null);
				}
				if (loadingDialog != null)
					loadingDialog.showLoadingDialog(mContext.getString(R.string.login_loading));
			}
		});
	}
	
	private String getAreaCode(){
		String code = mAreaCode.getText().toString();
		while (code.contains("0")) {
			int begin = code.indexOf("0");
			if (begin != -1)
				code = code.substring(begin+1);
		}
		
		return code;
	}
	
	@OnClick(R.id.phone_or_email)
	public void OnInputPhoneOrEmailClick(){
		mPhoneOrEmailInput.requestFocus();
	}
	
	@OnClick(R.id.password)
	public void OnInputPasswordClick(){
		mPasswordInput.requestFocus();
	}
	
	@OnClick(R.id.forget_password)
	public void onForgetPasswordClick(){
		Intent intent = new Intent(mContext, ForgetPasswordActivity.class);
		intent.putExtra("COMMON_TIPS", mContext.getString(R.string.common_forget_psd).toString());
		mContext.startActivity(intent);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (loadingDialog != null){
			loadingDialog.dismiss();
		}
		if (ChooseCountryReceiver != null)
			this.unregisterReceiver(ChooseCountryReceiver);
	}
	
	private void registerChooseCountryBroastcast(){
		IntentFilter intentFilter = new IntentFilter(); 
		intentFilter.addAction(PhoConstants.CHOOSE_COUNTRY_INFO_LOGIN);  
        this.registerReceiver(ChooseCountryReceiver, intentFilter);
	}
	
	BroadcastReceiver ChooseCountryReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(PhoConstants.CHOOSE_COUNTRY_INFO_LOGIN)) {
				mAreaCode.setText(intent.getStringExtra("AREA_CODE"));	
				mArea.setText(intent.getStringExtra("COUNTRY"));	
			}
		}
	};
	
	@SuppressWarnings("static-access")
	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		inputMethodManger.hideKeyboard(mPasswordInput);
		if (loadingDialog != null)
			loadingDialog.closeLoadingDialog();
		if (exception != null && !exception.getExMessage().equals("")){
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_SHORT).show();
		}else{
			finish();
		}
		
		
	}
}